package com.taichuan.selfcheck.mvp.presenter;


import android.media.AudioManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.taichuan.keysupport.Key;
import com.taichuan.mvp.base.MvpBasePresenter;
import com.taichuan.mvp.fragment.BaseFragmentManager;
import com.taichuan.selfcheck.mvp.viewinterface.MainInterface;
import com.taichuan.selfcheck.ui.fragment.MainFragment;
import com.taichuan.selfcheck.ui.fragment.MainFragment2;
import com.taichuan.selfcheck.ui.fragment.MainFragment3;

public class MainPresenter extends MvpBasePresenter<MainInterface> {
    float mPosX,mPosY,mCurPosX,mCurPosY;

    public boolean changeVolumeByTouch(MotionEvent event, AudioManager audioManager){
        switch (event.getAction()){
            //按下
            case MotionEvent.ACTION_DOWN:
                mPosX = mCurPosX = event.getX();
                mPosY = mCurPosY = event.getY();
                break;
            //移动
            case MotionEvent.ACTION_MOVE:
                mCurPosX = event.getX();
                mCurPosY = event.getY();
                break;
            //松开
            case MotionEvent.ACTION_UP:
                if (mCurPosY - mPosY > 0
                        && (Math.abs(mCurPosY - mPosY) > 150)) {
                    //向下滑動，下调音量
                    SubVolume(audioManager);
                    return true;
                } else if (mCurPosY - mPosY < 0
                        && (Math.abs(mCurPosY - mPosY) > 150)) {
                    //向上滑动，上调音量
                    AddVolume(audioManager);
                    return true;
                }
                break;
        }
        return false;
    }

    public void switchFunctionPageByTouch(MotionEvent event, BaseFragmentManager mBaseFragmentManager){
        switch (event.getAction()){
            //按下
            case MotionEvent.ACTION_DOWN:
                mPosX = mCurPosX = event.getX();
                mPosY = mCurPosY = event.getY();
                break;
            //移动
            case MotionEvent.ACTION_MOVE:
                mCurPosX = event.getX();
                mCurPosY = event.getY();
                break;
            //松开
            case MotionEvent.ACTION_UP:
                if (mCurPosX - mPosX > 0
                        && (Math.abs(mCurPosX - mPosX) > 250)) {
                    //向右滑動
                    switchFunctionPage(mBaseFragmentManager, false);
                } else if (mCurPosX - mPosX < 0
                        && (Math.abs(mCurPosX - mPosX) > 250)) {
                    //向左滑动
                    switchFunctionPage(mBaseFragmentManager, true);
                }
                break;
        }
    }
    private void AddVolume(AudioManager audio){
        audio.adjustStreamVolume(
                AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_RAISE,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
    }

    private void SubVolume(AudioManager audio){
        audio.adjustStreamVolume(
                AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_LOWER,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
    }

    public void switchFunctionPageByKeyEvent(KeyEvent event,BaseFragmentManager mBaseFragmentManager){
        if (event.getKeyCode() == Key.Convert.KEYCODE_0){
            switchFunctionPage(mBaseFragmentManager, true);
        }
    }

    /**
     * 切换功能页面
     *
     * @param leftToRight 是否从左到右
     */
    private void switchFunctionPage(BaseFragmentManager mBaseFragmentManager, boolean leftToRight) {
        if (leftToRight) {
            if (mBaseFragmentManager.getCurrentFragment() instanceof MainFragment) {
                mBaseFragmentManager.replace_remove_add(new MainFragment2());
            } else if (mBaseFragmentManager.getCurrentFragment() instanceof MainFragment2) {
                mBaseFragmentManager.replace_remove_add(new MainFragment3());
            } else if (mBaseFragmentManager.getCurrentFragment() instanceof MainFragment3) {
                mBaseFragmentManager.replace_remove_add(new MainFragment());
            }
        } else {
            if (mBaseFragmentManager.getCurrentFragment() instanceof MainFragment) {
                mBaseFragmentManager.replace_remove_add(new MainFragment3());
            } else if (mBaseFragmentManager.getCurrentFragment() instanceof MainFragment2) {
                mBaseFragmentManager.replace_remove_add(new MainFragment());
            } else if (mBaseFragmentManager.getCurrentFragment() instanceof MainFragment3) {
                mBaseFragmentManager.replace_remove_add(new MainFragment2());
            }
        }
    }
}
